package com.yxh.activity;

import java.util.LinkedHashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yxh.R;
import com.yxh.dto.UserDto;
import com.yxh.service.Task;
import com.yxh.util.Constant;

/**
 * update userInfo
 * 
 * @author luoming 
 * type :0 用户名称 性别 地区
 */
public class UserForUpdateInfoActivity extends BaseActivity implements
		OnClickListener {

	public static final int NAME_TYPE = 0; // 修改名字
	public static final int SEX_TYPE = 1; // 修改性别

	private String userName;
	private int type;
	private EditText userNameEt;
	private ImageView clearNameImg;
	private InputMethodManager inputMethodManager;
	/*** sex ***/
	private String sex;
	private ImageView sexImg1, sexImg2;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_usercenter_update_info);

		type = getIntent().getIntExtra("type", -1);

		if (type == NAME_TYPE) {
			initNameView();
		} else if (type == SEX_TYPE) {
			initSexView();
		} else {
			// 非法类型
		}

		overridePendingTransition(R.anim.enter_righttoleft, R.anim.noanim);
	}

	private void initNameView() {
		((TextView) findViewById(R.id.head_layout_tv)).setText("名字");
		findViewById(R.id.head_layout_right).setOnClickListener(this);
		findViewById(R.id.head_layout_right).setVisibility(View.VISIBLE);
		findViewById(R.id.head_layout_right_tv).setVisibility(View.VISIBLE);
		
		findViewById(R.id.update_username_layout).setVisibility(View.VISIBLE);

		userNameEt = (EditText) findViewById(R.id.update_username_et);
		clearNameImg = (ImageView) findViewById(R.id.update_username_img);
		clearNameImg.setOnClickListener(this);

		userName = getIntent().getStringExtra("userName");
		if (!TextUtils.isEmpty(userName)) {
			userNameEt.setText(userName);
			userNameEt.setSelection(userName.length());
		}

		inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				inputMethodManager.toggleSoftInput(0,
						InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}, 250);
	}

	private void initSexView() {
		sex = getIntent().getStringExtra("sex");
		((TextView) findViewById(R.id.head_layout_tv)).setText("性别");
		findViewById(R.id.update_sex_layout).setVisibility(View.VISIBLE);

		findViewById(R.id.update_sex_layout1).setOnClickListener(this);
		findViewById(R.id.update_sex_layout2).setOnClickListener(this);
		sexImg1 = (ImageView) findViewById(R.id.update_sex_img1);
		sexImg2 = (ImageView) findViewById(R.id.update_sex_img2);
		changeSexUI(Constant.SEX_MEN_KEY.equals(sex));

	}

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.head_layout_right) {
			goUpdateName();
		} else if (view.getId() == R.id.update_username_img) {
			goClearName();
		} else if (view.getId() == R.id.update_sex_layout1) {
			goUddateSex(Constant.SEX_MEN_KEY);
		} else if (view.getId() == R.id.update_sex_layout2) {
			goUddateSex(Constant.SEX_WOMEN_KEY);
		}
	}

	// 数据校验
	private void goUpdateName() {
		userName = String.valueOf(userNameEt.getText());
		if (TextUtils.isEmpty(userName)) {
			showToast("用户名不能为空");
			userNameEt.setFocusable(true);
			return;
		}

		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("s", "account/edit_userinfo");
		map.put("fieldname", "name");
		map.put("value", userName);
		getData(map, Task.EDIT_USER_NAME, 1);

	}

	private void goClearName() {
		userNameEt.setText("");
		userNameEt.setFocusable(true);

	}

	private void goUddateSex(String value) {
		changeSexUI(Constant.SEX_MEN_KEY.equals(value));
		if (value.equals(sex)) {
			finish();
			return;
		}
		Intent it = new Intent();
		it.putExtra("sex", value);
		setResult(RESULT_OK, it);
		finish();
	}

	@Override
	public void refresh(Object... param) {
		try {
			int operType = (Integer) param[0];
			Object obj = param[1];
			closeDialog();
			switch (operType) {
			case Task.EDIT_USER_NAME:
				if (obj != null) {
					UserDto dto = (UserDto) obj;
					if ("1".equals(dto.result)) {
						UserDto userInfo = getcurrentUser();
						userInfo.nickname = userName;
						db.insertUserInfo(userInfo);

						Intent it = new Intent();
						it.putExtra("userName", userName);
						setResult(RESULT_OK, it);
						finish();
					} else
						showToast(dto.message);
				} else {
					showToast(R.string.http_no_net_tip);
				}
				break;

			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void changeSexUI(boolean isBoy) {
		sexImg1.setEnabled(isBoy);
		sexImg2.setEnabled(!isBoy);
	}

}